package com.gxg.demo10.mydemo11.linechart;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.formatter.IndexAxisValueFormatter;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.gxg.demo10.mydemo11.R;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LineChartActivity extends AppCompatActivity implements OnChartValueSelectedListener {

    @Bind(R.id.chart1)
    LineChart chart;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_line_chart);
        ButterKnife.bind(this);

        //设置数值选择监听
        chart.setOnChartValueSelectedListener(this);
        chart.setDrawGridBackground(false);
        // 没有描述的文本
        chart.getDescription().setEnabled(false);
        chart.setDrawBorders(false);

        chart.getAxisLeft().setEnabled(true);
        chart.getAxisRight().setEnabled(true);
        chart.getAxisRight().setDrawAxisLine(false);
        chart.getAxisRight().setDrawLabels(false);
        chart.getAxisRight().setDrawGridLines(false);
        chart.getXAxis().setEnabled(true);
        chart.getXAxis().setDrawAxisLine(true);
        chart.getXAxis().setDrawGridLines(false);
        chart.getXAxis().setDrawLimitLinesBehindData(true);


        // 支持触控手势
        chart.setTouchEnabled(true);

        // 支持缩放和拖动
        chart.setDragEnabled(true);
        chart.setScaleEnabled(true);

        // 如果禁用,扩展可以在x轴和y轴分别完成
        chart.setPinchZoom(true);

        // 设置背景颜色(灰色)
//        chart.setBackgroundColor(Color.BLACK);

        Legend l = chart.getLegend();
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
        l.setOrientation(Legend.LegendOrientation.VERTICAL);
        l.setDrawInside(false);



        initData();
    }

    public String[] titles = {"执法一队",
            "执法二队",
            "执法三队",
            "执法四队",
            "执法五队",
    };

    public String[] values = {"一月",
            "二月",
            "三月",
            "四月",
            "五月",
            "六月",
            "七月",
            "八月",
            "九月",
            "十月",
            "十一月",
            "十二月",
    };
    private final int[] colors = new int[]{
            ColorTemplate.VORDIPLOM_COLORS[0],
            ColorTemplate.VORDIPLOM_COLORS[1],
            ColorTemplate.VORDIPLOM_COLORS[2]
    };

    private void initData() {
        chart.resetTracking();
        ArrayList<ILineDataSet> dataSets = new ArrayList<>();
        for (int i = 0; i < titles.length; i++) {
            ArrayList<Entry> values = new ArrayList<>();
            for (int j = 0; j < 12; j++) {
                int a = (int) (Math.random() * 100) + 10;
                if(i==3 && j>10){
//                    values.add(new Entry(j, a));
                }else{
                    values.add(new Entry(j, a));
                }

            }

            LineDataSet d = new LineDataSet(values, titles[i]);
            d.setLineWidth(1.5f);
            d.setCircleRadius(3f);

            int color = colors[i % colors.length];
            d.setColor(color);
            d.setCircleColor(color);
            d.setDrawCircles(true);
            d.setDrawValues(true);
            dataSets.add(d);

        }

        ((LineDataSet) dataSets.get(0)).enableDashedLine(10, 10, 0);
//        ((LineDataSet) dataSets.get(0)).setColors(ColorTemplate.VORDIPLOM_COLORS);
//        ((LineDataSet) dataSets.get(0)).setCircleColors(ColorTemplate.VORDIPLOM_COLORS);

        LineData data = new LineData(dataSets);

        //自定义x轴显示
        MyXFormatter formatter = new MyXFormatter(values);
        XAxis xAxis = chart.getXAxis();
        xAxis.setGranularity(1);
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawAxisLine(true);
        chart.setData(data);
        chart.setVisibleXRangeMaximum(12);
        //设置悬浮
        xAxis.setDrawGridLines(false);
        xAxis.setDrawLimitLinesBehindData(true);
        //显示个数
        xAxis.setLabelCount(12);
        xAxis.setValueFormatter(formatter);
        XYMarkerView mv = new XYMarkerView(this, formatter);
        mv.setChartView(chart);
        chart.setMarker(mv);
        chart.invalidate();
    }

    @Override
    public void onValueSelected(Entry e, Highlight h) {

    }

    @Override
    public void onNothingSelected() {
        //setOnChartValueSelectedListener
    }

    public class MyXFormatter extends ValueFormatter {

        private String[] mValues;

        public MyXFormatter(String[] values) {
            this.mValues = values;
        }

        private static final String TAG = "MyXFormatter";

        @Override
        public String getFormattedValue(float value) {
            if (value >= 0) {
                return mValues[(int) value % mValues.length];
            } else {
                return "";
            }

        }
    }



}

